<template>
  <div class="ads-add-or-edit">
    <el-card>
      <div slot="header">{{ isEdit ? '编辑广告位' : '新增广告位' }}</div>
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="广告位名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('form')"
              >提交</el-button
            >
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { saveOrUpdateSpace, getSpaceById } from '@/service/ads-space'
export default {
  props: {
    spaceId: {
      type: [Number, String]
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        name: ''
      },
      rules: {
        name: [{ required: true, message: '请输入广告位名称', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.spaceId && this.loadSpace()
  },
  methods: {
    async loadSpace () {
      const { data } = await getSpaceById({ id: this.spaceId })
      if (data.state === 1) {
        this.form = data.content
      }
    },
    async submitForm (formName) {
      const valid = await this.$refs[formName].validate()
      if (valid) {
        const { data } = await saveOrUpdateSpace(this.form)
        if (data.state === 1) {
          this.$message.success('提交成功')
          this.$router.push({ name: 'ads-space' })
        }
      } else {
        console.log('error submit!!')
        return false
      }
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
// ::v-deep .el-card__header {
//   text-align: left;
// }
::v-deep .el-form-item__content {
  text-align: left;
}
</style>
